#!/usr/bin/python
#coding:utf-8

#author: Freev

import threading
import requests
import linecache
import re
import sys

def is_git(host,num):
    try:
        s=requests.get('http://'+host+'/.git/index',allow_redirects = False,timeout=3,verify=False)
        su=s.status_code
        html=s.text
        if su==200 and get_real(html[0:4]):
            print ('%d :%s ok'%(num,host))
            gitfile.write(host+'\n')
        else: print ('%d :%s no really'%(num,host))
    except Exception,ex:
         print ('%d :%s %s'%(num,host,ex))

def get_real(html):
   # print html
    if html=='DIRC':
        return True
    return False

def git_host(host):
    s=requests.get('http://'+host,allow_redirects = False,timeout=2)
    su=s.status_code

def getlin(li):
    con=linecache.getline(hostpath,li).strip('\n\r\t')
    if con: return con
    else: return None

def main():
    x=0
    a=len(linecache.getlines(hostpath))
    threads=[]
    th=30
    while x<a:
        flag=0
        for i in range(x,th+x):
            if getlin(i+1):
                flag+=1
                temp=threading.Thread(target=is_git,args=(getlin(i+1),i))
                threads.append(temp)
            else: break
        for i in range(x,len(threads)):
            threads[i].start()
        for i in range(x,len(threads)):
            threads[i].join()
        x=x+th

if __name__ =='__main__':


    if len(sys.argv)==3:
        hostpath=sys.argv[1]
        gitpath=sys.argv[2]
       
        gitfile=open(gitpath,'a')
        main()
    else:  print "python  GitTesting.py    host.txt    git.txt "